﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCon;

namespace yfxt
{
    public partial class gl_ypsl : Form
    {
        public gl_ypsl()
        {
            InitializeComponent();
        }

        public string lykf;
        string comStr;
        public string lingyaodanhao;
        int yksb;
        int tijiao_var = 0;               // 变量，控制“提交”按钮是否执行代码
        int queren_var = 0;               // 变量，控制“确认”按钮是否执行代码
        int delete_var = 0;               // 变量，控制“删除”按钮是否执行代码
        int queren0_var = 0;               // 变量，控制“确认0”按钮是否执行代码

        #region 刷新函数              ---显示左和右DatagridView
        private void xianshi_DatagridView()
        {
            #region 显示左边DataGridView
            DataTable dt1 = new DataTable();
            comStr = "select ckdh,SQRQ,CASE sqtj WHEN 0 THEN '未提交' when 1 then '已提交' END,CASE ckpb WHEN 0 THEN '未确认' when 1 then '已确认' END,ckbz "+
                "from Yk_cK01 where lypb=0 order by sqtj,ckpb";
            dt1.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView1.Rows.Clear();
            if (dt1.Rows.Count > 0)
            {
                dataGridView1.Rows.Add(dt1.Rows.Count);
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    dataGridView1.Rows[i].Cells[0].Value = dt1.Rows[i][0].ToString();
                    dataGridView1.Rows[i].Cells[1].Value = Convert.ToDateTime(dt1.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView1.Rows[i].Cells[2].Value = dt1.Rows[i][2].ToString();
                    dataGridView1.Rows[i].Cells[3].Value = dt1.Rows[i][3].ToString();
                    dataGridView1.Rows[i].Cells[4].Value = dt1.Rows[i][4].ToString();
                }
            }
	        #endregion

            #region 显示右边DataGridView
            DataTable dt2 = new DataTable();
            comStr = "select ckdh,lyrq,ckbz from Yk_cK01 where lypb=1";
            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
            dataGridView2.Rows.Clear();
            if (dt2.Rows.Count > 0)
            {
                dataGridView2.Rows.Add(dt2.Rows.Count);
                for (int i = 0; i < dt2.Rows.Count; i++)
                {
                    dataGridView2.Rows[i].Cells[0].Value = dt2.Rows[i][0].ToString();
                    dataGridView2.Rows[i].Cells[1].Value = Convert.ToDateTime(dt2.Rows[i][1]).ToString("yyyy-MM-dd").Replace('-', '.');
                    dataGridView2.Rows[i].Cells[2].Value = dt2.Rows[i][2].ToString();
                }
            }
        		 
	        #endregion

            #region datagridView默认不选中单元格，使之成蓝色
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows[0].Selected = false;
            }
            if (dataGridView2.Rows.Count > 0)
            {
                dataGridView2.Rows[0].Selected = false;
            }
            #endregion
        }
        #endregion

        private void gl_ypsl_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView2.AllowUserToResizeRows = false;                     //禁止用鼠标调整datagridView单元格的大小。
            dataGridView2.AllowUserToResizeColumns = false;
            foreach (DataGridViewColumn c in dataGridView1.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            foreach (DataGridViewColumn c in dataGridView2.Columns)        //禁止对datagridView列进行排序
            {
                c.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            //dataGridView1.CurrentCell = null;
            //入库方式
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = new DataTable();
            if (mainform.yfsb==2)    //2表示中药房
            {
                yksb = 2;
            }
            else
            {
                yksb = 1;
            }
            comStr = "select ykmc from Yk_YKLB where yksb=" + yksb + "";
            string ykmc=(SqlHelper.ExecuteScalar(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null)).ToString();
            comboBox1.Items.Add(ykmc);
            comboBox1.Text = ykmc;

            xianshi_DatagridView();

        }

        /// <summary>
        /// 增加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            gl_ypsld ypsld = new gl_ypsld();
            ypsld.Owner = this;
            lykf = comboBox1.Text.ToString();
            ypsld.ShowDialog();
        }

        /// <summary>
        /// 选择单元格，弹出领药单信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            lingyaodanhao = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            gl_ypsld_zuo ypsld_zuo = new gl_ypsld_zuo();
            ypsld_zuo.Owner = this;
            ypsld_zuo.ShowDialog();
        }

        private void dataGridView2_KeyDown(object sender, KeyEventArgs e)
        {
            lingyaodanhao = dataGridView2.CurrentRow.Cells[0].Value.ToString();
            gl_ypsld_you ypsld_you = new gl_ypsld_you();
            ypsld_you.Owner = this;
            ypsld_you.ShowDialog();
        }

        /// <summary>
        /// datagridView失去焦点，单元格不显示蓝色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {

                dataGridView1.Rows[i].Selected = false;
            }
        }

        private void dataGridView2_Leave(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView2.Rows.Count; i++)
            {

                dataGridView2.Rows[i].Selected = false;
            }
        }


        /// <summary>
        /// 提交按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow.Cells[2].Value.ToString() == "未提交")
                {
                    if (MessageBox.Show("是否提交领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            comStr = "update Yk_cK01 set sqtj='1',czgh='" + Program.ygbh + "',sqrq='"+DateTime.Now.ToString()+"' where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            xianshi_DatagridView();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow.Cells[2].Value.ToString() == "未提交")
                {
                    if (MessageBox.Show("是否删除领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", 
                        "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        string Delete_ckdh = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                        try
                        {
                            string comStr = "delete from Yk_cK01 where ckdh='" + Delete_ckdh + "';" +
                                "delete from Yk_cK02 where ckdh='" + Delete_ckdh + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                            xianshi_DatagridView();
                            MessageBox.Show("删除领药单" + "" + Delete_ckdh + "" + "成功！");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.CurrentRow.Cells[2].Value.ToString() == "已提交" && dataGridView1.CurrentRow.Cells[3].Value.ToString() == "已确认")
                {
                    if (MessageBox.Show("是否确认申领单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        try
                        {
                            comStr = "update Yk_cK01 set lypb='1',lygh='" + Program.ygbh + "',lyrq='" + DateTime.Now.ToString() + "' where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                            SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);

                            //药房库存表 增加库存                                
                            comStr = "select t.sfsl,t1.yfsb,t.ypxh,t.ypcd,t.ypph,t.ypxq,t.lsjg,t.lsje from Yk_cK02 t,Yk_cK01 t1 " +
                                "where t.ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "' and t1.ckdh=t.ckdh";
                            DataTable dt2 = new DataTable();
                            dt2.Load(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null));
                            for (int i = 0; i < dt2.Rows.Count; i++)
                            {
                                decimal sfsl = Convert.ToDecimal(dt2.Rows[i][0]);
                                string yfsb = dt2.Rows[i][1].ToString();
                                string ypxh = dt2.Rows[i][2].ToString();
                                string ypcd = dt2.Rows[i][3].ToString();
                                string ypph = dt2.Rows[i][4].ToString();
                                string ypxq = dt2.Rows[i][5].ToString();
                                string lsjg = dt2.Rows[i][6].ToString();
                                string lsje = dt2.Rows[i][7].ToString();
                                //update，yf_kcmx的库存
                                comStr = "update t1 set t1.ypsl=t1.ypsl+'" + sfsl + "' from yf_kcmx t1 where " +
                                "t1.ypxh='" + ypxh + "' and t1.yfsb='" + yfsb + "' and t1.ypph='" + ypph + "'";
                                int res = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                                //如果update不到，insert， yf_kcmx的库存
                                if (res < 1)
                                {
                                    comStr = "insert into yf_kcmx(ypph,ypsl,yfsb,ypxh,ypcd,ypph,ypxq,lsjg,lsje) select max(ypph)+1,'" + sfsl + "'," +
                                        "'" + yfsb + "','" + ypxh + "','" + ypcd + "','" + ypph + "','" + ypxq + "','" + lsjg + "','" + lsje + "' from yf_kcmx)";
                                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                                }
                            }
                            xianshi_DatagridView();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message.ToString());
                        }
                    }
                    //else
                    //{
                    //    queren_var = 0;
                    //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
                    //    {

                    //        dataGridView1.Rows[i].Selected = false;
                    //    }
                    //}
                }
            }
        }

        //矩形框变成红色
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics,
                              panel1.ClientRectangle,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed,
                              Color.Red,
                              1,
                              ButtonBorderStyle.Dashed); 

            //ControlPaint.DrawBorder(e.Graphics,
            //                   panel1.ClientRectangle,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid,
            //                   Color.Red,
            //                   3,
            //                   ButtonBorderStyle.Solid); 
        }

        /// <summary>
        /// 药库确认药房的领药单 button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button7_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0)
            {
                if (dataGridView1.Focus() && dataGridView1.CurrentRow.Cells[2].Value.ToString() == "已提交")
                {
                    if (MessageBox.Show("是否确认药房领药单" + "" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "" + "？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        comStr = "update Yk_cK01 set CKPB='1',ckrq='" + DateTime.Now.ToString() + "',qrgh='" + Program.ygbh + "' " +
                            "where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "';" +
                            "update Yk_cK02 set sfsl=sqsl where ckdh='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
                        SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, comStr, null);
                        xianshi_DatagridView();
                    }
                }
            }
        }

        /// <summary>
        /// 刷新按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            xianshi_DatagridView();
        }
    }
}
